#pragma once
#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QMessageBox>
#include <QTcpSocket>
#include "recvfile.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    m_s = new QTcpServer(this);

    connect(m_s,&QTcpServer::newConnection,this,[=](){
        QTcpSocket* tcp = m_s->nextPendingConnection();

        // 使用传参方式传递给子线程
        RecvFile* subThread = new RecvFile(tcp);
        subThread->start();

        // 因为子线程在此创建，所以子线程发送的信号也要在此处进行处理
        connect(subThread,&RecvFile::over,this,[=](){
            subThread->exit();
            subThread->wait();
            subThread->deleteLater();
            QMessageBox::information(this,"文件接收","文件接收完毕！！！");
        });
    });

}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_setListen_clicked()
{
    unsigned short port = ui->port->text().toUShort();
    m_s->listen(QHostAddress::Any,port);
    ui->setListen->setText("已启用监听");
    ui->setListen->setDisabled(true);
}
